package boot.controller.wxUser;

import boot.common.CommonResult;
import boot.domain.address.Address;
import boot.domain.wxGoodsCart.GoodsShopcart;
import boot.model.wxResEntity;
import boot.service.addressService;
import boot.service.wxUserService;
import boot.util.JwtUtil;
import com.alibaba.fastjson.JSON;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
public class userAddress {
    @Autowired
    private boot.service.wxUserService wxUserService;
    @Autowired
    private addressService addressService;
    @PostMapping("/wxUser/addAddress")
    public CommonResult addAddress(@RequestHeader("Authorization") String token, @RequestParam("province") String province,@RequestParam("city") String city,@RequestParam("detailAddress") String detailAddress,@RequestParam("name") String name,@RequestParam("phoneNum") String phoneNum,@RequestParam("town") String town) {
        try {
            Claims claims = JwtUtil.parseJWT(token);
            assert claims != null;
            wxResEntity wxResEntity = JSON.parseObject(claims.getSubject(), boot.model.wxResEntity.class);
            String openid = wxResEntity.getOpenid();
            int userId = wxUserService.getIdByOpenId(openid);
            Long phoneNum1 = Long.parseLong(phoneNum);
            Address address = new Address(null, userId, province, city, detailAddress, name, phoneNum1,town);
            addressService.addAddressByUserId(address);
            return new CommonResult(200, "添加成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(400, "添加失败", null);
        }
    }
    @PostMapping("/wxUser/editAddress")
    public CommonResult editAddress(@RequestHeader("Authorization") String token, @RequestParam("province") String province,@RequestParam("city") String city,@RequestParam("detailAddress") String detailAddress,@RequestParam("name") String name,@RequestParam("phoneNum") String phoneNum,@RequestParam("town") String town,@RequestParam("id") int id) {
        try {
            Claims claims = JwtUtil.parseJWT(token);
            assert claims != null;
            wxResEntity wxResEntity = JSON.parseObject(claims.getSubject(), boot.model.wxResEntity.class);
            String openid = wxResEntity.getOpenid();
            int userId = wxUserService.getIdByOpenId(openid);
            Long phoneNum1 = Long.parseLong(phoneNum);
            Address address = new Address(null, userId, province, city, detailAddress, name, phoneNum1,town);
            addressService.editAddressByUserId(address,id);
            return new CommonResult(200, "修改成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(400, "修改失败", null);
        }
    }
    @GetMapping("/wxUser/getAddress")
    public CommonResult<List<Address>> getAddress(@RequestHeader("Authorization") String token) {
        try {
            Claims claims = JwtUtil.parseJWT(token);
            assert claims != null;
            wxResEntity wxResEntity = JSON.parseObject(claims.getSubject(), boot.model.wxResEntity.class);
            String openid = wxResEntity.getOpenid();
            int userId = wxUserService.getIdByOpenId(openid);
            List<Address> addresses = addressService.getAddressByUserId(userId);
            return new CommonResult<>(200, "添加成功", addresses);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<>(400, "添加失败", null);
        }
    }
    @GetMapping("/wxUser/deleteAddress")
    public CommonResult<List<Address>> deleteAddress(@RequestHeader("Authorization") String token,@RequestParam("id") int id) {
        try {
            Claims claims = JwtUtil.parseJWT(token);
            assert claims != null;
            wxResEntity wxResEntity = JSON.parseObject(claims.getSubject(), boot.model.wxResEntity.class);
            String openid = wxResEntity.getOpenid();
            int userId = wxUserService.getIdByOpenId(openid);
            addressService.deleteAddressById(id);
            return new CommonResult<>(200, "添加成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<>(400, "添加失败", null);
        }
    }
}
